package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.model.Message;
import com.qy.service.MessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by zaq on 2018/07/10.
 */
@RestController("Manage-MessageController")
@RequestMapping("/manage/message")
public class MessageController {
    @Resource
    private MessageService messageService;

    @PostMapping("/add")
    public Result add(@RequestBody Message message) {
        messageService.save(message);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        messageService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Message message) {
        messageService.update(message);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Message message = messageService.findById(id);
        return ResultGenerator.successResult(message);
    }

    @GetMapping("/list")
    public Result list(PageBean<Message> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Message> list = messageService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //系统消息
    @GetMapping("/messageList")
    public Result messageList(PageBean<Map> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize(), "add_time desc");
        List<Map> list = messageService.messageList();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //系统消息-阅读
    @PostMapping("/seeMessage")
    public Result seeMessage(@RequestBody Message message){
        if (message.getId() == null || message.getId() == 0){
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        messageService.seeMessage(message);
        return ResultGenerator.successResult();
    }

    //系统消息-批量删除
    @GetMapping("/deleteMessages")
    public Result deleteMessages(String ids){
        if (ids == null || ids.isEmpty()){
            throw new ServiceException("请至少选择一条进行删除");
        }
        messageService.deleteByIds(ids);
        return ResultGenerator.successResult();
    }

}
